﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace Skyworth.CRM.Web
{
    public class BasePage : System.Web.UI.Page
    {
        //protected Dictionary<System.Web.UI.HtmlControls.HtmlInputButton, int> dic = new Dictionary<System.Web.UI.HtmlControls.HtmlInputButton, int>();
        //protected string themeName = "default";

        protected void Page_Init(object sender,EventArgs e)
        {
            //HttpCookie themeCookie = Request.Cookies["dwz_theme"];
            //themeName = themeCookie.Value;
            SystemOverTime();
        }
       
        public int UserType
        {
            get {
                return Session["UserType"] != null ? Convert.ToInt32(Session["UserType"]) : 1;
            }
        }

        /// <summary>
        /// --------------------------------------------------------------
        /// 属性名称:IndividualId
        /// 功能描述:登录的用户对应的实际编号，若为用户则对应CustomerId,
        /// 若为员工则对应EmpId
        /// --------------------------------------------------------------
        /// </summary>
        public int IndividualId
        {
            get
            {
                if (Session["IndividualId"] != null)
                {
                    return Convert.ToInt32(Session["IndividualId"]);
                }
                else
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// --------------------------------------------------------------
        /// 属性名称:UserId
        /// 功能描述:登录的用户编号
        /// --------------------------------------------------------------
        /// </summary>
        public string UserID
        {
            get
            {
                if (Session["UserID"] != null)
                {
                    return Session["UserID"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// --------------------------------------------------------------
        /// 属性名称:UserName
        /// 功能描述:登录的用户名称
        /// --------------------------------------------------------------
        /// </summary>
        public string UserName
        {
            get
            {
                if (Session["UserName"] != null)
                {
                    return Session["UserName"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// --------------------------------------------------------------
        /// 属性名称:FuncModelList
        /// 功能描述:用户对应的功能菜单
        /// --------------------------------------------------------------
        /// </summary>
        public DataTable FuncModelList
        {
            get
            {
                if (Session["FuncModelList"] != null)
                {
                    return (DataTable)Session["FuncModelList"];
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// --------------------------------------------------------------
        /// 属性名称:ControlAuthority
        /// 功能描述:按钮权限
        /// --------------------------------------------------------------
        /// </summary>
        /// <summary>
        /// --------------------------------------------------------------
        /// 属性名称:ControlAuthority
        /// 功能描述:按钮权限
        /// --------------------------------------------------------------
        /// </summary>
        public void ControlAuthority<T>(T ctrl, int pageId) where T : IList<WebControl>
        {
            if (ctrl.Count > 0)
            {
                foreach (WebControl t in ctrl)
                {
                    switch (t.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.Button":
                            DataRow[] drs1 = FuncModelList.Select("ModuleID=" + pageId + " and FunctionID=" + ((Button)t).CommandArgument);
                            t.Enabled = (drs1.Length > 0);
                            break;
                        case "System.Web.UI.WebControls.LinkButton":
                            DataRow[] drs2 = FuncModelList.Select("ModuleID=" + pageId + " and FunctionID=" + ((LinkButton)t).CommandArgument);
                            t.Enabled = (drs2.Length > 0);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        
        /// <summary>
        /// ------------------------------------------------------------
        /// 方法名称：BinDropDownList
        /// 功能描述：绑定数据到控件
        /// ------------------------------------------------------------
        /// </summary> 
        public void BinDropDownList<T>(T lt,DataTable dt, string DefaultName,string DefaultValue) where T : ListControl
        {
            lt.Items.Clear();
            lt.DataSource = dt;
            lt.DataBind();
            if(!string.IsNullOrEmpty(DefaultName) || !string.IsNullOrEmpty(DefaultValue))
                lt.Items.Insert(0, new ListItem(DefaultName, DefaultValue));
        }

        /// <summary>
        /// ------------------------------------------------------------
        /// 方法名称：BinDropDownList
        /// 功能描述：在控件中查找固定值并选中
        /// ------------------------------------------------------------
        /// </summary>    
        public void BinDropDownList<T>(T lt, string curValue, bool isEnabled) where T : ListControl
        {            
            //lt.SelectedItem.Selected = false;
            string[] strs = curValue.Split(',');
            for (int i = 0; i < strs.Length; i++)
            {
                ListItem ltItem = lt.Items.FindByValue(strs[i]);
                if (ltItem != null)
                {
                    ltItem.Selected = true;
                }                
            }
            lt.Enabled = isEnabled;
        }



        /// <summary>
        /// ---------------------------------------------------------------
        /// 方法名称:SystemOverTime
        /// 功能描述:判断当前Session是否过期，如果过期，则弹出提示，重新登陆
        /// ---------------------------------------------------------------   
        /// </summary>
        public void SystemOverTime()
        {
            if (string.IsNullOrEmpty(this.UserID) || string.IsNullOrEmpty(this.UserID) || this.FuncModelList == null)
            {
                StringBuilder strScript =new StringBuilder( "<script language=\"javascript\">");
                strScript.Append("parent.location.href='"+"http://"+Request.Url.Authority+ResolveUrl("~/login.aspx")+"';");
                strScript.Append("</script>");
                Response.Write(strScript.ToString());
                Response.End();
            }
        }

        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="strInfo"></param>
        protected void Alert(string strInfo)
        {
            #region
            string js = @"<Script language='JavaScript'>
                    alert('" + strInfo + "');</Script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", js);
            #endregion
        }


        /// <summary>
        /// 绑定部门树形结构到DropdownList
        /// </summary>
        /// <param name="dtDepartment">部门DataTable,须包含DepartmentId，DepartmentName 字段</param>
        /// <param name="parentId"></param>
        /// <param name="ddlDept"></param>
        protected void BindTreeViewToDropdownList(DataTable dtDepartment, int parentId, DropDownList ddlDept,int count,string nameField,string valueField)
        {
            if (dtDepartment != null && dtDepartment.Rows.Count > 0)
            {
                DataRow[] drRoots = dtDepartment.Select("ParentId = " + parentId);
                count++;
                foreach (DataRow row in drRoots)
                {
                    int deptId = 0;
                    int.TryParse(row["DepartmentId"].ToString(), out deptId);
                    StringBuilder strText = new StringBuilder(string.Empty);
                    
                    for (int i=0;i<count;i++)
                    {
                        strText.Append(Server.HtmlDecode("&nbsp;&nbsp;&nbsp;"));
                    }
                    strText.Append(row[nameField].ToString());
                    ddlDept.Items.Add(new ListItem(strText.ToString(), row[valueField].ToString()));

                    BindTreeViewToDropdownList(dtDepartment, deptId, ddlDept, count, nameField,valueField);
                }
                
                
            }

        }
    }
}